﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.ProductInfo;

namespace com.subb.ProductInfo
{
    public class PP_ProductCataDAL
    {
        public List<PP_ProductCata> GetAllProductCata(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_ProductCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProdCataID);
                ttlCount = query.Count();
                List<PP_ProductCata> pcatalist = query.Skip(start).Take(limit).ToList();
                return pcatalist;
            }
        }

        public List<PP_ProductCata> GetAllProductCata()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.PP_ProductCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProdCataID);
                List<PP_ProductCata> pcatalist = query.ToList();
                return pcatalist;
            }
        }

        public PP_ProductCata GetProductCata(int cataId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                PP_ProductCata pcata = ctx.PP_ProductCata.SingleOrDefault(c => c.ProdCataID == cataId);
                return pcata;
            }
        }

        public PP_ProductCata GetFirstProductCata()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                var query = ctx.PP_ProductCata.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.ProdCataID);
                PP_ProductCata cata1 = query.FirstOrDefault();
                return cata1;
            }
        }

        public void CreateProductCata(ProductCata v_pcata)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_ProductCata ppcata = new PP_ProductCata
                {
                    ProdCataName = v_pcata.ProdCataName,
                    ParentCataID = v_pcata.ParentCataID,
                    CataDescr = v_pcata.CataDescr,
                    CreateTime = DateTime.Now,
                    Creator = v_pcata.Creator,
                    SortNum = v_pcata.SortNum
                };
                ctx.PP_ProductCata.AddObject(ppcata);
                ctx.SaveChanges();
            }
        }

        public bool UpdateProductCata(int pcId, ProductCata v_pcata)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_ProductCata ppcata = ctx.PP_ProductCata.SingleOrDefault(c => c.ProdCataID == pcId);
                if (ppcata != null)
                {
                    ppcata.ProdCataName = v_pcata.ProdCataName;
                    ppcata.ParentCataID = v_pcata.ParentCataID;
                    ppcata.CataDescr = v_pcata.CataDescr;
                    ppcata.SortNum = v_pcata.SortNum;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveProductCata(int pcId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_ProductCata ppcata = ctx.PP_ProductCata.SingleOrDefault(c => c.ProdCataID == pcId);
                if (ppcata != null)
                {
                    if (ppcata.PP_Product != null)
                    {
                        List<PP_Product> prodList = ppcata.PP_Product.ToList();
                        foreach (var prod in prodList)
                        {
                            prod.ProdCataID = null;
                        }
                    }
                    ctx.PP_ProductCata.DeleteObject(ppcata);
                    ctx.SaveChanges();
                }
            }
            return result;
        }
    }
}
